<?php
 
namespace app\common\job;
 
use Yii;
use yii\base\BaseObject;
use app\helpers\DatetimeHelper;
use yii\helpers\ArrayHelper;
use app\modules\notify\service\NotifyService;
use app\modules\notify\service\NotifyRangeService;
use app\modules\notify\service\NotifyLatestService;
use app\modules\notify\service\NotifyRangeLatestService;
use \yii\queue\Job;
 
class NotifySpecialUpdateJob extends BaseObject implements Job
{
    public $rangeList;
    public $notifyId;
    public $createTimestamp;
    public $updateTimestamp;

    public function execute($queue){
        $rangeList = $this->rangeList;
        $notifyId = $this->notifyId;
        $createTimestamp = $this->createTimestamp;
        $updateTimestamp = $this->updateTimestamp;
        
        $range = [];
        foreach($rangeList as $key => $temp) {
            $range[$key]['notify_id'] = $notifyId;
            $range[$key]['userid'] = $temp['user_id'];
            $range[$key]['content'] = $temp['content'];
            $range[$key]['create_timestamp'] = $createTimestamp;
            $range[$key]['update_timestamp'] = $updateTimestamp;;
            $range[$key]['is_read'] = 0;
        }
        
        self::callModuleService('notify', 'NotifyRangeService', 'updateAll', ['status' => 0], ['notify_id' => $notifyId]);
        self::callModuleService('notify', 'NotifyRangeService', 'saveMany', $range, ['notify_id', 'userid']);
        self::callModuleService('notify', 'NotifyRangeLatestService', 'updateAll', ['status' => 0], ['notify_id' => $notifyId]);
        self::callModuleService('notify', 'NotifyRangeLatestService', 'saveMany', $range, ['notify_id', 'userid']);
        
        //完成所有工作，修改进度
        (new NotifyService)->update(['process'=>2],['id' => $notifyId]);    //阅读中
        (new NotifyLatestService)->update(['process'=>2],['id' => $notifyId]);
    }
}